/*LICENSE_BEGIN
 * ******************************************************************
 * Copyright 2006 by the Dainamite Robocup-Team at the
 * DAI-Labor, TU-Berlin (www.dai-labor.de)
 * 
 * Members of Dainamite are:
 * Holger Endert
 * Robert Wetzker
 * Thomas Karbe
 * Axel Heßler
 * Philippe Bütter
 * Felix Brossmann
 * 
 * Licensed under the GNU GPL (General Public License) Free Software 
 * License, Version 2. You may use this file only in compliance with 
 * the License. You may obtain a copy of the License at:
 * 
 * http://www.fsf.org/licensing/licenses/gpl.txt
 * ******************************************************************
LICENSE_END*/
package robocup.component.coach;

import java.util.Vector;

import robocup.component.DAInamiteConstants;
import robocup.component.infotypes.AuralInfo;
import robocup.component.infotypes.CoachVisualInfo;

/**
 * CoachFactBase is a database with unmodified informations 
 * from the world.
 */
public class CoachFactBase {

	/**
	 * A list containing a history of visual information, with initial
	 * size <code>DAInamiteConstants.FB_HIST_SIZE_C</code>.
	 */
	private Vector<CoachVisualInfo> visualHistory = new Vector<CoachVisualInfo>(
			DAInamiteConstants.FB_HIST_SIZE_C);

	/**
	 * A list containing a history of aural information, with initial
	 * size <code>DAInamiteConstants.FB_HIST_SIZE_C</code>.
	 */
	private Vector<AuralInfo> auralHistory = new Vector<AuralInfo>(
			DAInamiteConstants.FB_HIST_SIZE_C);

	/**
	 * The latest visual information
	 */
	private CoachVisualInfo currentCVinfo = null;

	/**
	 * The latest aural information
	 */
	private AuralInfo currentAinfo = null; // Current AuralInfo

	/**
	 * Returns the current CoachVisualInfo
	 * 
	 * @return current CoachVisualInfo
	 */
	public CoachVisualInfo getCurrentVI() {
		return this.currentCVinfo;
	}

	/**
	 * <code>getCurrentAinfo</code> getter
	 * 
	 * @return Returns the currentAinfo.
	 */
	public AuralInfo getCurrentAI() {
		return this.currentAinfo;
	}

	/**
	 * <code>update</code> backups old CoachVisualInfo. If the lists size
	 * is already greater or equal to <code>DAInamiteConstants.FB_HIST_SIZE_C
	 * </code>, the first (oldest) element is dropped. The new info object
	 * replaces the <code>currentCVinfo</code> as well.
	 * 
	 * @param info - the new visual info, which should be stored.
	 */
	public void update(CoachVisualInfo info) {
		if (info != null) {
			// Remove the last element if the CoachVisualInfo history is full
			if (DAInamiteConstants.FB_HIST_SIZE_C <= this.visualHistory.size())
				this.visualHistory.remove(this.visualHistory.lastElement());

			// Backup old CoachVisualInfo
			this.visualHistory.add(0, this.currentCVinfo);
			// Update current CoachVisualInfo
			this.currentCVinfo = info;
		} // end if
	}

	/**
	 * <code>update</code> updates the factbase after the arrival of an
	 * AuralInfo. If the lists size is already greater or equal to 
	 * <code>DAInamiteConstants.FB_HIST_SIZE_C</code>, the first (oldest) 
	 * element is dropped. The new info object replaces the <code>
	 * currentAinfo</code> as well.
	 * 
	 * @param info -
	 *            new aural info
	 */
	public void update(AuralInfo info) {
		if (info != null) {
			// Remove the last element if the CoachVisualInfo history is full
			if (DAInamiteConstants.FB_HIST_SIZE_C <= this.auralHistory.size())
				this.auralHistory.remove(this.auralHistory.lastElement());

			// Backup old CoachVisualInfo
			this.auralHistory.add(0, this.currentAinfo);
			// Update current CoachVisualInfo
			this.currentAinfo = info;
		}
	}

	/**
	 * 
	 * Get old AuralInfos, if information about requested info no longer exists
	 * the oldest available is returned
	 * 
	 * @param n -
	 *          steps/cycles back in time
	 * @return
	 * 			the aural info of the actual cycle - n, or oldest, if history
	 * 			isn't that long. 
	 */
	public AuralInfo getPreviousAI(int n) {
		if (n == 0) {
			return this.currentAinfo;
		}
		if (n > this.auralHistory.size()) {
			return this.auralHistory.lastElement();
		}
		return this.auralHistory.elementAt(n - 1);
	}

	/**
	 * 
	 * Get old VisualInfos, if information about requested info no longer exists
	 * the oldest available is returned
	 * 
	 * @param n
	 *          steps/cycles back in time
	 * @return
	 * 			the aural info of the actual cycle - n, or oldest, if history
	 * 			isn't that long. 
	 */
	public CoachVisualInfo getPreviousVI(int n) {
		if (n == 0) {
			return this.currentCVinfo;
		}
		if (n > this.visualHistory.size()) {
			return this.visualHistory.lastElement();
		}
		return this.visualHistory.elementAt(n - 1);
	}
}
